package com.wechat.page.chat;

import com.swak.fx.support.AbstractPage;
import com.swak.fx.support.FXMLView;
import com.swak.fx.support.ui.UI;
import com.swak.fx.support.ui.UIAction;
import com.swak.fx.support.ui.UIIcon;
import com.swak.fx.support.ui.UISearchBar;
import com.swak.fx.support.ui.UIStackPane;
import com.wechat.dto.ChatDTO;
import com.wechat.page.chat.list.ListPage;

import javafx.beans.property.LongProperty;
import javafx.beans.property.ObjectProperty;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;

/**
 * ChatListPage.
 * 
 * @author 618lf
 */
@FXMLView(title = "聊天列表", value = "ChatListPage.fxml", css = "ChatListPage.css")
public class ChatListPage extends AbstractPage {

	@FXML
	private GridPane searchPane;
	@FXML
	private UIStackPane contentPane;

	// ========================
	// 搜索
	// ========================
	private UISearchBar searchInput;
	private UIAction addGroupBtn;

	// ========================
	// 列表
	// ========================
	private ListPage listPage;

	public ChatListPage() {
		this.listPage = this.NewPage(ListPage.class);
	}

	@Override
	public void initializeParts() {
		super.initializeParts();
		this.initializeSearchParts();

		this.contentPane.setPrefHeight(0);
		this.contentPane.getChildren().add(this.listPage.getRoot());
		GridPane.setVgrow(contentPane, Priority.ALWAYS);
		GridPane.setHgrow(contentPane, Priority.ALWAYS);
	}

	private void initializeSearchParts() {
		this.searchInput = new UISearchBar();
		this.addGroupBtn = new UIAction();

		this.searchInput.clear();
		this.searchInput.getStyleClass().add("search");
		this.addGroupBtn.textNodeProperty().set(new UIIcon(UI.Icons.PLUS).Tip("添加群聊"));
		this.addGroupBtn.getStyleClass().add("add-group-btn");
		this.searchPane.add(searchInput, 0, 0);
		this.searchPane.add(addGroupBtn, 1, 0);
		GridPane.setHgrow(searchInput, Priority.ALWAYS);
		GridPane.setHgrow(searchPane, Priority.ALWAYS);
		GridPane.setMargin(searchInput, new Insets(0, 10, 0, 0));
	}

	@Override
	public void setupEventHandlers() {
		super.setupEventHandlers();
		this.registerUIEvent(searchPane, UI.Events.Window_Drag);
		this.root.setOnDragOver((e) -> {
			e.acceptTransferModes(TransferMode.ANY);
		});
		this.root.setOnDragDropped((e) -> {
			TransferMode mode = e.getAcceptedTransferMode();
			if (mode == TransferMode.MOVE) {
			}
		});
	}

	// ================================================================================
	// Properties
	// ================================================================================
	public ObjectProperty<ChatDTO> activeProperty() {
		return this.listPage.activeProperty();
	}

	public LongProperty refreshProperty() {
		return this.listPage.refreshProperty();
	}
}